using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Unit_Item_Ctrl : Unit_Base
{
    public override UnitType unitType =>  UnitType.Item;

    public enum ItemType
    {
        Default,
        Follow,
    }
    public ItemType itemType;

    public string itemName;
    public List<string> tags;

    public Collider2D trigger;
    public Unit_Base source_Ctrl;

    public bool isFollowLight;
    public float followLightDistance = 3;
    public float followForce=1.2f;
    public Rigidbody2D rigibody;
    private void Start()
    {
        EventManager.Instance.Regist(CharactorControlType.EmitLight.ToString(), this.GetInstanceID(), (objects) =>
        {
            if (isFollowLight&& rigibody && this.gameObject.activeSelf && (Player_Ctrl.instance.charactor_Ctrl.transform.position - this.transform.position).magnitude < followLightDistance)
            {
                Vector3 direction = Player_Ctrl.instance.charactor_Ctrl.transform.position - this.transform.position;
                RaycastHit2D[] hits = Physics2D.RaycastAll(this.transform.position, direction, followLightDistance+1);
                foreach(RaycastHit2D hit in hits)
                {
                    if(LayerMask.LayerToName(hit.collider.gameObject.layer)=="Wall"|| LayerMask.LayerToName(hit.collider.gameObject.layer) == "Floor")
                    {
                        break;
                    }
                    if (hit.collider.GetComponentInParent<Unit_Charactor_Ctrl>())
                    {
                        rigibody.velocity = (direction.normalized + Vector3.up * 0.2f) * direction.magnitude * followForce;
                        break;
                    }
                }  
            }
        });
    }

    protected override void OnUnitTriggerEnter(Unit_Base unit_Base)
    {
        switch (unit_Base.unitType)
        {
            case UnitType.Charactor:
                if (source_Ctrl == null)
                {
                    source_Ctrl = unit_Base;
                    Unit_Charactor_Ctrl charactor_Ctrl = (Unit_Charactor_Ctrl)unit_Base;
                    switch (itemType)
                    {
                        case ItemType.Default:
                            if(tags.Contains("Shell") || itemName == "Shell"){
                                EventHandler_Prop.CallShowChellEvent(transform.position);
                            }
                            this.gameObject.SetActive(false);
                            break;
                        case ItemType.Follow:
                            charactor_Ctrl.ownItems.Add(this);
                            trigger.enabled = false;
                            if (rigibody)
                            {
                                rigibody.velocity = Vector3.zero;
                            }
                            if(tags.Contains("SleepGrass1") || tags.Contains("SleepGrass2")){
                                EventHandler_Prop.CallSleepGrassFollowEvent(this.gameObject, rigibody);
                            }else if(tags.Contains("VomitGrass")){
                                EventHandler_Prop.CallCharacterGetVoimitGrassEvent();
                            }
                            break;
                    }
                    EventManager.Instance.DispatchEvent(CharactorControlType.GetItem.ToString(), itemName);
                }
                break;
        }

    }
    private void OnDisable()
    {
        if (source_Ctrl != null)
        {
            switch (source_Ctrl.unitType)
            {
                case UnitType.Charactor:
                    Unit_Charactor_Ctrl charactor_Ctrl = (Unit_Charactor_Ctrl)source_Ctrl;
                    if (charactor_Ctrl.ownItems.Contains(this))
                    {
                        charactor_Ctrl.ownItems.Remove(this);
                    }
                    break;
            }
           
        }
    }
    private void OnDestroy()
    {
        EventManager.Instance.UnRegist(CharactorControlType.EmitLight.ToString(), this.GetInstanceID());
        if (source_Ctrl != null)
        {
            switch (source_Ctrl.unitType)
            {
                case UnitType.Charactor:
                    Unit_Charactor_Ctrl charactor_Ctrl = (Unit_Charactor_Ctrl)source_Ctrl;
                    if (charactor_Ctrl.ownItems.Contains(this))
                    {
                        charactor_Ctrl.ownItems.Remove(this);
                    }
                    break;
            }
        }
    }
}
